var express = require('express');
var router = express.Router();
//引入Query.js连接数据库
const Query = require('../tools/Query')


// /* GET home page. */
//开始页面 
router.get('/', function (req, res, next) {
  res.send('Express');
});

// 首页banner（首页轮播图的接口）
router.get("/banner", async (req, res, next) => {
  let sql = `SELECT id,title,CONCAT("http://localhost:3000/uploads/",image_src) AS image_src FROM e_banners`;
  
  let [, result] = await Query(sql)
  let data={
    msg:"ok",
    status:200,
    result
  }
  res.send(data)
})


//course的接口
router.get("/courseinfo", async (req, res) => {

  //type类型的展示
  let {type = 1} = req.query
  //limit限制
  let limit = type==1?6:8

  console.log(type);
  let sql = `SELECT CONCAT('http://localhost:3000/','uploads/','course/',image_src)AS image_src,subject_name,area_name,type,title,cid,id,type_name FROM e_course WHERE type=${type} LIMIT ${limit}`;
  //结构数据
  let result = await Query(sql)
  res.send(result)
})




router.use("/banner",(req,res,next)=>{
next({msg:"404"})
})
//暴露数据
module.exports = router;
